// pages/addList/addList.js
const req = require('../../utils/apifm-wxapi')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr:[],
    index:0,
    provinceId:'',
    city:[],
    cityIndex:0,
    cityId:'',
    county:[],
    countyIndex:0,
    countyId:'',
    name:'',
    Iphone:'',
    address:'',
    flag:false,
    addId:''
  },
  changeCountry(e){
    console.log(e)
    var arr =this.data.arr
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      provinceId:arr[e.detail.value].id
    })
    console.log(this.data.provinceId)
    var pid =this.data.provinceId
    req.nextRegion(pid).then((res)=>{
      console.log(res)
      let obj ={
        name:"请选择"
      }
      var city =res.data
      city.unshift(obj)
      this.setData({
        city:city
      })
    })
  },
  changeCity(e){
    console.log(e)
    var arr =this.data.city
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      cityIndex:e.detail.value,
      cityId:arr[e.detail.value].id
    })
    var pid=this.data.cityId
    req.nextRegion(pid).then((res)=>{
      console.log(res)
      let obj ={
        name:"请选择"
      }
      var county =res.data
      county.unshift(obj)
      this.setData({
        county: county
      })
    })

  },
  changecounty(e){
    console.log(e)
    var arr =this.data.county
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      countyIndex:e.detail.value,
      countyId:arr[e.detail.value].id
    })
  },
  change(e){
    console.log(e.detail.value)
    this.setData({
      name:e.detail.value
    })
  },
  Iphone(e){
    console.log(e.detail.value)
    this.setData({
      Iphone:e.detail.value
    })
  },
  address(e){
    console.log(e.detail.value)
    this.setData({
      address:e.detail.value
    })
  },
  save(){
    var Token = wx.getStorageSync('Token')
    let obj ={
      address:this.data.address,
      cityId:this.data.cityId,
      linkMan:this.data.name,
      mobile:this.data.Iphone,
      provinceId:this.data.provinceId,
      districtId:this.data.countyId,
      token:Token,
      id:this.data.addId
    }
    req.updateAddress(obj).then((res)=>{
      console.log(res)
      if(res.code==0){
        wx.showToast({
          title: '修改成功',
        })
        wx.navigateTo({
          url: '/pages/address/address',
        })
        wx.hideToast()
      }
    })
  },
  del(){
    var Token = wx.getStorageSync('Token')
    var id=this.data.addId
    req.deleteAddress(Token,id).then((res)=>{
      if(res.code==0){
        wx.showToast({
          title: '删除成功',
        })
        wx.navigateTo({
          url: '/pages/address/address',
        })
        wx.hideToast()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 全部省份
    console.log(options.item)
    this.setData({
      addId:options.item
    })
    req.province().then((res)=>{
      console.log(res.data)
      let obj ={
        name:"请选择"
      }
      var arr =res.data
      arr.unshift(obj)
      this.setData({
        arr:arr
      })
    })
    // 获取到的数据
    var Token =wx.getStorageSync('Token')
    req.addressDetail(Token,options.item).then((res)=>{
      console.log(res.data.info)
      var arr=this.data.arr
      for(var i=0;i<arr.length;i++){
        if(res.data.info.provinceId==arr[i].id){
          console.log(i)
          this.setData({
            index:i
          })
        }
      }
      this.setData({
        name:res.data.info.linkMan,
        Iphone:res.data.info.mobile,
        address:res.data.info.address,
      })
      console.log(this.data.name)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})